import pygame, math, sys, random
from priest import Priest
from zombie import Zombie
from map import Level
from health import HealthBar
from Block import Block
from menu import Button

pygame.init()

if pygame.font:
    pygame.font.init()

if pygame.mixer:
    pygame.mixer.init()

clock = pygame.time.Clock()

screenWidth = 800
screenHeight = 600
#got from Chris, Jackson, Tyler
fullscreen = 0
altFlag = False
                                                               # YELLOW #RED #BLUE 
  #>>>>>> Load Startscreen <<<<<<                 #250 #250     #200  #10
singleplayer = Button("Fight for your religion", [200,150], (200, 20, 10))#10    #10
                                                          #250    #400
exit = Button("Turn against your religion and run!!!!!", [25,300], (200, 40, 50))

bgColor = 255,255,255
#-------------------------------------------------------------------------------
screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode(screenSize)

player = Priest(5, [screenWidth/2, screenHeight-100])
zombies = []
maxZombie = 10

healthbar = HealthBar([screenWidth - 75, 75]) #DEFAULT: 100 MODED: 200
                                    #600

map = Level("map", screenSize)

bgColor = 0,0,0

Overimg = pygame.image.load("gameover.png")
OverRect = Overimg.get_rect()

run = False

while True:
    while not run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP or event.key == pygame.K_w and not singleplayer.highlighted:
                    	singleplayer.highlighted = True
                    	exit.highlighted = False
                elif event.key == pygame.K_DOWN or event.key == pygame.K_s and not exit.highlighted:
                    	exit.highlighted = True
                    	singleplayer.highlighted = False
                elif event.key == pygame.K_SPACE and exit.highlighted == True:
                    sys.exit()
                elif event.key == pygame.K_SPACE and singleplayer.highlighted == True:
                    run = True
            elif event.type == pygame.MOUSEMOTION:
                if singleplayer.collidePt(event.pos):
                    singleplayer.highlighted = True
                    exit.highlighted = False
                elif exit.collidePt(event.pos):
                    singleplayer.highlighted = False
                    exit.highlighted = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    if singleplayer.collidePt(event.pos):
                        singleplayer.clicked = True
                        run = True
                    elif exit.collidePt(event.pos):
                        exit.clicked = True
                        sys.exit()
                    
            
        singleplayer.update((200, 10, 10))
        exit.update((200, 10, 10))
        
        screen.fill(bgColor)
        screen.blit(singleplayer.surface, singleplayer.rect)
        screen.blit(exit.surface, exit.rect)
        pygame.display.flip()
    while run and player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_UP
                    or event.key == pygame.K_w):
                        player.direction("up")
                elif (event.key == pygame.K_DOWN
                    or event.key == pygame.K_s):
                        player.direction("down")
                elif (event.key == pygame.K_RIGHT
                    or event.key == pygame.K_d):
                        player.direction("right")
                elif (event.key == pygame.K_LEFT
                    or event.key == pygame.K_a):
                        player.direction("left")
                elif (event.key == pygame.K_h):
                        player.attack("punch")
                elif (event.key == pygame.K_j):
                        player.attack("punchR")
                elif (event.key == pygame.K_k):
                        player.attack("belch")
            elif event.type == pygame.KEYUP:
                if (event.key == pygame.K_UP
                    or event.key == pygame.K_w):
                        player.direction("stop up")
                elif (event.key == pygame.K_DOWN
                    or event.key == pygame.K_s):
                        player.direction("stop down")
                elif (event.key == pygame.K_RIGHT
                    or event.key == pygame.K_d):
                        player.direction("stop right")
                elif (event.key == pygame.K_LEFT
                    or event.key == pygame.K_a):
                        player.direction("stop left")
                elif (event.key == pygame.K_k):
                        player.attack("stop belch")
                elif (event.key == pygame.K_d):
                        player.attack("stop chomp")
            
        #------------FULLSCREEN-----------------------------------------------------------
                #got from Chris, Jackson, Tyler
                elif (event.key == pygame.K_RALT or event.key == pygame.K_LALT):
                    altFlag = True
                elif (event.key == pygame.K_RETURN) and altFlag:
                    if fullscreen == 0:
                        fullscreen = pygame.FULLSCREEN
                    else:
                        fullscreen = 0
                    screen = pygame.display.set_mode((screenWidth,screenHeight),
                    							      fullscreen)
                    pygame.display.flip()


       #>>>>>> Menu <<<<<<

                    
        #---Spawn Zombies---------------------  
        if len(zombies) < maxZombie:
            if random.randint(1,10)  == 1:
                zpos = [random.randint(100,screenWidth-100),
                    		random.randint(100,screenHeight/2)]
                zombies += [Zombie(zpos)]
                insideSomething = True
                while insideSomething:
                    insideSomething = False
                    for block in map.blocks:
                        if block.distToPoint(zombies[-1].rect.center) < 20:
                            zpos = [random.randint(100,screenWidth-100),
                                    random.randint(100,screenHeight/2)]
                            zombies[-1].place(zpos)
                            insideSomething = True
                    if player.collide_point(zpos, 40):
                        zpos = [random.randint(100,screenWidth-100),
                                    random.randint(100,screenHeight/2)]
                        zombies[-1].place(zpos)
                        insideSomething = True
                    for zombie in zombies[:-1]:
                        if zombie.collide_zombie(zombies[-1]):
                            zpos = [random.randint(100,screenWidth-100),
                                        random.randint(100,screenHeight/2)]
                            zombies[-1].place(zpos)
                            insideSomething = True
                

        #-------Move------
        player.move()
        for zombie in zombies:
            zombie.move(player)
        for block in map.blocks:
            block.playerCollide(player)
        for zombie in zombies:
            for block in map.blocks:
                if block.distToPoint(zombie.rect.center) < 20:
                    block.zombieCollide(zombie)
        
        #for block in map.blocks:
         #   block.enemyCollide(zombies)
        
        #-----Collide------
        player.collide_wall(screenWidth, screenHeight)
                
        for zombie in zombies:
            zombie.didHit = False
            zombie.collide_wall(screenWidth,screenHeight)
            zombie.collide_fatpriest(player)
            player.enemyCollide(zombie,healthbar)
            if player.punching:
                zombie.collide_attack(player.punch)
            if player.belching:
                zombie.collide_attack(player.belch)
            if player.punchingR:
                zombie.collide_attack(player.punchR)
            zombie.healthbar.update()
                 #zombie.collide_zombie(zombie)
            #if zombie.chase(priest):
              #for first in range(0,len(zombies)-2):
                #for second in range(first+1, len(zombies)-1):
            #if player.belching:
             # zombie.collide_beltch(player.beltch)
        
        for first in range(0,len(zombies)-2):
            for second in range(first+1, len(zombies)-1):
                zombies[first].collide_zombie(zombies[second])
                       
        #------Remove Dead------
        for zombie in zombies:
            if not zombie.living:
                zombies.remove(zombie)
                
        if player.health <= 0:
            player.living = False
            
         # adding blocks 
        screen.fill(bgColor) 
        for block in map.fblocks:
          screen.blit(block.surface, block.rect)     
        for block in map.blocks:
          screen.blit(block.surface, block.rect)    
        for block in map.dblocks:  
          screen.blit(block.surface, block.rect)
    #    for block in map.mblocks:  
    #      screen.blit(block.surface, block.rect)
    
   
        for block in map.blocks:
            screen.blit(block.surface,block.rect)
        for zombie in zombies:
            screen.blit(zombie.surface, zombie.rect)
            screen.blit(zombie.healthbar.surface, zombie.healthbar.rect)
        screen.blit(player.surface, player.rect)
        if player.belching:
            screen.blit(player.belch.surface, player.belch.rect)
        if player.punching:
            screen.blit(player.punch.surface, player.punch.rect)
        if player.punchingR:
            screen.blit(player.punchR.surface, player.punchR.rect)
        screen.blit(healthbar.surface, healthbar.rect)
        pygame.display.flip()
        clock.tick(30)
    
        #--------------FAT-PRIEST-DEAD---------------------
    while player.living == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_RETURN):
                    #player.retry("yes", [500, 400])
                    pass
             
        screen.blit(Overimg, OverRect)
        pygame.display.flip()

                
                
        #------Blit-------
        
        
    
    
